package net.falcon;

import net.falcon.data.Arena;

import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.kitteh.tag.PlayerReceiveNameTagEvent;

public class PlayerEventListener implements Listener {

	@EventHandler
	public void onPlayerMove(PlayerMoveEvent e) {
		Arena a = Util.getArena(e.getPlayer());
		if(a != null) {
			a.playerMoved(e);
		}
	}

	@EventHandler
	public void onDeath(PlayerDeathEvent e) {
		Arena a = Util.getArena(e.getEntity());
		if(a != null) {
			a.playerDied(e);
		}

	}

	@EventHandler
	public void onBlockBreak(BlockBreakEvent e) {
		Arena a = Util.getArena(e.getPlayer());
		if(a != null) {
			a.blockBroken(e);
		}
	}

	@EventHandler
	public void onTagChange(PlayerReceiveNameTagEvent event){
		if(Util.getMetadata(event.getNamedPlayer(), "fa.map") != null) {
			event.setTag("§§§§");
		}
	}

	//stop breaking paintings, item frames
	@EventHandler
	public void onHangingBreak(HangingBreakEvent e) {
		if(e.getCause() == RemoveCause.ENTITY) {
			HangingBreakByEntityEvent ev = (HangingBreakByEntityEvent)e;
			if(ev.getRemover() instanceof Player) {
				Player p = (Player)ev.getRemover();
				if(Util.getMetadata(p, "fa.map") != null) {
					e.setCancelled(true);
				}
			}
			if(ev.getRemover() instanceof Projectile) {
				e.setCancelled(true);
			}
		}
		if(e.getCause() == RemoveCause.EXPLOSION) {
			e.setCancelled(true);
		}
	}

}
